/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package GUI;

import java.net.DatagramSocket;
import java.util.ArrayList;

/**
 *
 * @author Windows7
 */
public class taskGame implements subject, Runnable {
    
    private DatagramSocket socket;
    private int port;
    private ArrayList<user> allPlayer;

    public taskGame() {
    }

    public taskGame(DatagramSocket socket, int port, ArrayList<user> allPlayer) {
        this.socket = socket;
        this.port = port;
        this.allPlayer = allPlayer;
    }

    public taskGame(taskGame  t) {
        this.socket = t.socket;
        this.port = t.port;
        this.allPlayer = t.allPlayer;
    }
    public taskGame(DatagramSocket socket) {
        this.socket = socket;
        this.port = socket.getPort();
        this.allPlayer = new ArrayList<>();
    }
    

    @Override
    public void registerObserver(user o) {
        synchronized(this.allPlayer)
        {
            this.allPlayer.add(o);
        }
    }

    @Override
    public void removeObserver(user o) {
        synchronized(this.allPlayer)
        {
            int index = this.allPlayer.indexOf(o);
            if(index >= 0)
            {
                this.allPlayer.remove(index);
            }
        }
    }

    @Override
    public void notifyAllObserver() {
        synchronized(allPlayer)
        {
            int n = this.allPlayer.size();
            for(int i = 0; i < n ; i++)
            {
                Thread t = new Thread((Runnable) this.allPlayer.get(i));
                t.start();
            }
        }
    }

    @Override
    public void run() {
        
    }

    public int getPort() {
        return port;
    }

    public void setPort(int port) {
        this.port = port;
    }
    
    
    
}

